package top.ljlgithub.api.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.tomcat.jni.OS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.ljlgithub.api.bean.*;
import top.ljlgithub.api.common.response.Meta;
import top.ljlgithub.api.common.response.ResponseEntity;
import top.ljlgithub.api.exception.ParamException;
import top.ljlgithub.api.service.OrderService;

/**
 * @ClassName
 * @Description TODO
 * @Author L1470
 * @Date 2021/5/7 16:43
 * @Version 1.0
 **/
@RestController
@CrossOrigin
public class OrderController {
    @Autowired
    OrderService orderService;

    @RequiresPermissions("user:getAllOrders:*")
    @GetMapping("/orders")
    public ResponseEntity getOrderList(PageCondition pageCondition) throws ParamException {
        String query = pageCondition.getQuery();
        Integer pagesize = pageCondition.getPageSize();
        Integer pagenum = pageCondition.getPageNum();
        if (pagesize == null || pagenum == null){
            throw new ParamException();
        }
        Meta meta = new Meta("获取成功", 200);
        OrderParam orderParam = new OrderParam(query, pagenum, pagesize);
        OrderPageBean orderList = orderService.getOrderList(orderParam);
        ResponseEntity<Object> result = new ResponseEntity<>(orderList,meta);
        return result;
    }

    @RequiresPermissions("user:getOrder:*")
    @GetMapping("/orders/{id}")
    public ResponseEntity getOrderById(@PathVariable("id") Integer id){
        Meta meta = new Meta("获取成功", 200);
        OrderGoods orderGoods = orderService.findOrderById(id);
        ResponseEntity<Object> result = new ResponseEntity<>(orderGoods,meta);
        return result;
    }

    @RequiresPermissions("user:updateOrder:*")
    @PutMapping("/orders/{id}")
    public ResponseEntity updateOrder(@PathVariable("id") Integer id,@RequestBody Order order){
        order.setOrder_id(id);
        Meta meta = new Meta("修改成功", 200);
        orderService.updateOrder(order);
        OrderGoods orderGoods = orderService.findOrderById(order.getOrder_id());
        ResponseEntity<Object> result = new ResponseEntity<>(orderGoods,meta);
        return result;
    }
}
